<?php
class MessageModel extends Model {
	
	public function addMessage($map){
		
		$user  = getMember();
		$query = session(C('USER_SELECT_CITY'));
		$tody  = date("Y-m-d");
		$begin = strtotime($tody." 00:00:01");
		$end   = strtotime($tody." 23:59:59");
		
		$cat['dataid']  = $user['id'];
		$cat['tableid'] = $user['type'];
		$cat['restid']  = $map['restid'];
		$cat['addtime'] = array(array('gt',$begin),array('lt',$end));
		
		$view = $this->where($cat)->find();
		
		if($view['id']){
			return -1;
		}else{
			$map['dataid']  = $user['id'];
			$map['tableid'] = $user['type'];
			$map['city']    = $query['cityid'];
			$map['addtime']  = time();
			$data = $this->add($map);
			return $data;
		}
	}
	
	public function mesgselect($map){
		
		$desc     = $this->_req("orderDirection", "trim",   "desc");;
		$order    = $this->_req("orderName",      "trim",   "addtime");
		$pageNum  = $this->_req("pageNum",        "intval", C('PAGE_NUMS'));
		$nowPage  = $this->_req("p",              "intval", 1);
		$restid   = $this->_req("restid",         "intval", 0);
		$where    = $this->_req('where');
		 
		if($where){
			foreach ($where as $k=>$v){
				if($v){
					$map[$k] = $v;
				}
			}
		}
		 
		$map['isdel'] = 2 ;
		$con = $order.' '.$desc;
		
		$data = $this->pageSelect($map,$pageNum,$nowPage,$con);
	
		return $data;
	}
	
	/**
	 * 分页公共方法
	 * @access private
	 * @param string   $table    表名
	 * @param array    $map      查询条件
	 * @param string   $order    结果排序
	 * @param int      $pageNum  每页数量
	 * @param int      $nowPage  当前页数
	 * @return array
	 */
	private function pageSelect($map,$pageNum,$nowPage,$con){
	
		$count = $this->where($map)->count();
	
		$list  = $this->where($map)->order($con)->page($nowPage.','.$pageNum)->select();
		//echo $model->getLastSql();
		return array("count"=>$count, "list"=>$list);
	}
}
?>